package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.crm.domain.pojo.CrmDevice;
import com.yu.crm.domain.pojo.Station;
import com.yu.crm.mapper.StationMapper;
import com.yu.crm.service.IStationService;
import com.yxzz.common.exception.GeneralException;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zds
 * @since 2021-03-12
 */
@Service
public class StationService extends ServiceImpl<StationMapper, Station> implements IStationService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public void deleteByIds(String ids) {
        try {
            String substring = ids.substring(0, ids.length());
            String[] result = substring.split(",");
            List<Long> longList = new ArrayList<>();
            for (String s : result) {
                Station station = baseMapper.selectById(Long.valueOf(s));
                if (DataHelpers.isNotEmpty(station)) {
                    station.setIsDeleted(1);
                    ValidatorHelpers.validateDataSave(baseMapper.updateById(station));
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    public Station infoById(Long id) {
        try {
            return baseMapper.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    /**
     * 删除车站信息,软删除
     *
     * @param id
     */
    public void deleteById(Long id) {
        try {
            Station station = baseMapper.selectById(id);
            if (DataHelpers.isNotEmpty(station)) {
                station.setIsDeleted(1);

                ValidatorHelpers.validateDataSave(baseMapper.updateById(station));
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
            throw new GeneralException("操作失败");
        }
    }

    public List<Station> getPageList(String stationName, String condition) {
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        if (DataHelpers.isNotEmptyString(stationName)) {
            queryWrapper.eq("station_name", stationName);
        }
        if (DataHelpers.isNotEmptyString(condition)) {
            queryWrapper.and(i -> i.like("station_name", condition)
            );
        }
        return baseMapper.selectList(queryWrapper);
    }

    public IPage<Station> getPage(Page<Station> page, String stationName, String condition) {
        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted", 0);
        if (DataHelpers.isNotEmptyString(stationName)) {
            queryWrapper.eq("station_name", stationName);
        }
        if (DataHelpers.isNotEmptyString(condition)) {
            queryWrapper.and(i -> i.like("station_name", condition)
            );
        }
        return baseMapper.selectPage(page, queryWrapper);
    }

    public List<String> getStationName() {
        List<Station> stations = baseMapper.selectList(new QueryWrapper<Station>()
                .eq("is_deleted", 0)
                .select("DISTINCT station_name"));
        return stations.stream().map(Station::getStationName).collect(Collectors.toList());
    }

}
